package org.jeecg.common.util.blockchain;

import org.beykery.eth.WillWallet;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.WalletUtils;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.RemoteCall;
import org.web3j.protocol.core.methods.response.EthGetBalance;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.core.methods.response.Web3ClientVersion;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.Contract;
import org.web3j.tx.gas.ContractGasProvider;
import org.web3j.tx.gas.DefaultGasProvider;

import java.math.BigInteger;

/**
 * check
 */
public class MyTokenMain {
    /**
     * 检查所有账户的erc20持有情况
     *
     * @param args
     */
    public static final void main(String... args) throws Exception {

      //  testDeploy();
        //test3();

        //trans();
    }


    public static void testDeploy() {
        //    String privateKey = args[0];//这里填入你的eth私钥
        //    String priceString = args[1];//这里填入你的价格（单位gwei），可以去 https://ethgasstation.info 查找一个SafeLow建议值
        // 29404234801161523843559422366258777649391763167937715829396789465576954978599
        //0x4abb1cbdeba4fd293c508442c59b3477ac551f1d
        try {
            String privateKey = "18C9916F28E17CDFD8869D4CF1D5A2C3FDAC6FBD77CEF6305033EB4DFFAD4DB9";//这里填入你的eth私钥
            String priceString = "4";//这里填入你的价格（单位gwei），可以去 https://ethgasstation.info 查找一个SafeLow建议值 8 3.3 2


            WillWallet wallet = new WillWallet(privateKey);
            String url = "https://ropsten.infura.io/v3/720246bcdf6046708fb18848c6b2bb63";
            Web3j web3j = Web3j.build(new HttpService(url));
            Web3ClientVersion web3ClientVersion = web3j.web3ClientVersion().sendAsync().get();
            String clientVersion = web3ClientVersion.getWeb3ClientVersion();
            System.out.println(clientVersion);
            EthGetBalance bl = web3j.ethGetBalance(wallet.getAddress(), DefaultBlockParameterName.LATEST).send();
            System.out.println("账上eth余额 : " + bl.getBalance());

            BigInteger gwei = new BigInteger("1000000000");
            BigInteger price = gwei.multiply(new BigInteger(priceString));
            BigInteger limit = new BigInteger("4800000");
            ContractGasProvider contractGasProvider = new DefaultGasProvider();
            RemoteCall<MyToken_sol_MyToken> call = MyToken_sol_MyToken.deploy(web3j, wallet.getCredentials(),contractGasProvider, new BigInteger("100000000"), "duping token", "duping");//TestToken_sol_TestToken可以改为你自己的合约
            MyToken_sol_MyToken tt = call.send();
            System.out.println("部署合约 " + tt.getContractAddress() + " 成功 .");//0x8ae55d4045838c0ad8d103e16a5c830741238d69
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void test3() throws Exception {
        //        //调用合约
        String privateKey = "18C9916F28E17CDFD8869D4CF1D5A2C3FDAC6FBD77CEF6305033EB4DFFAD4DB9";//这里填入你的eth私钥
        String priceString = "4";//这里填入你的价格（单位gwei），可以去 https://ethgasstation.info 查找一个SafeLow建议值 8 3.3 2
        WillWallet wallet = new WillWallet(privateKey);
        String url = "https://ropsten.infura.io/v3/720246bcdf6046708fb18848c6b2bb63";
        Web3j web3j = Web3j.build(new HttpService(url));
        String contractAddress = "0x45e3719ea2316e021e6a605b0d74ac5dd623f7f4";
        BigInteger gwei = new BigInteger("1000000000");
        BigInteger price = gwei.multiply(new BigInteger(priceString));
        BigInteger limit = new BigInteger("4800000");
        MyToken_sol_MyToken contract = MyToken_sol_MyToken.load(contractAddress, web3j, wallet.getCredentials(), price, limit);
        System.out.println("现在的值：" + contract.name().send().toString());
//        TransactionReceipt transactionReceipt = contract.set("nihaoshijie").send();
//        String value=contract.get().send();
//        System.out.println("现在的值："+value);
    }

    /**
     * 部署标准合约
     *
     * @param c           合约类
     * @param privateKey  私钥
     * @param priceString 价格（gwei）
     */
    public static void deploy(Class<? extends Contract> c, String privateKey, String priceString) throws Exception {
        WillWallet wallet = new WillWallet(privateKey);
        String url = "https://ropsten.infura.io/v3/720246bcdf6046708fb18848c6b2bb63";
        Web3j web3j = Web3j.build(new HttpService(url));
        Web3ClientVersion web3ClientVersion = web3j.web3ClientVersion().sendAsync().get();
        String clientVersion = web3ClientVersion.getWeb3ClientVersion();
        System.out.println(clientVersion);
        EthGetBalance bl = web3j.ethGetBalance(wallet.getAddress(), DefaultBlockParameterName.LATEST).send();
        System.out.println("账上eth余额 : " + bl.getBalance());

        BigInteger gwei = new BigInteger("1000000000");
        BigInteger price = gwei.multiply(new BigInteger(priceString));
        BigInteger limit = new BigInteger("4800000");
        ContractGasProvider contractGasProvider = new DefaultGasProvider();
        RemoteCall<MyToken_sol_MyToken> call = MyToken_sol_MyToken.deploy(web3j, wallet.getCredentials(), contractGasProvider, new BigInteger("10000"), "MYTOKEN", "MYTOEKM");
        MyToken_sol_MyToken tt = call.send();
        System.out.println("部署合约 " + tt.getContractAddress() + " 成功 .");
    }


    public static void trans(String password, String filePath, String toAddress, BigInteger amount, String contractAddress) {
        try {
            String url = "https://ropsten.infura.io/v3/720246bcdf6046708fb18848c6b2bb63";
            Web3j web3j = Web3j.build(new HttpService(url));
            Credentials credentials = WalletUtils.loadCredentials(password, filePath);
            MyToken_sol_MyToken myToken_sol_myToken = MyToken_sol_MyToken.load(contractAddress, web3j, credentials, new DefaultGasProvider());
            TransactionReceipt receipt = myToken_sol_myToken.transfer(toAddress, amount).send();
            System.out.println("转账txid： " + receipt.getTransactionHash() + " 成功 .");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("转账错误!!!!!");
        }
    }

}
